package logic

import (
	"context"
	"study/internal/model"

	"study/internal/svc"
	"study/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateUserLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateUserLogic {
	return &CreateUserLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateUserLogic) CreateUser(req *types.CreateUserRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	user := &model.User{
		Age:   req.Age,
		Name:  req.Name,
		Email: req.Email,
	}

	result := l.svcCtx.DB.Create(user).Error
	if result != nil {
		return &types.Response{
			Message: result.Error(),
		}, result
	}

	return &types.Response{
		Message: "添加成功",
	}, nil

	return
}
